package com.changgou.goods.controller;


import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌
     *
     * @return
     */
    @GetMapping
    public Result findAll() {

        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询所有品牌成功！", brandList);
    }

    /**
     * 查询所有品牌
     *
     * @return
     */
    @PostMapping("/search")
    public Result findByExample(@RequestBody Brand brand) {
        List<Brand> brandList = brandService.findByExample(brand);
        return new Result(true, StatusCode.OK, "查询所有品牌成功！", brandList);
    }

    /**
     * 分页查询品牌
     *
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageInfo pageInfo = brandService.findByPage(pageNum, pageSize);
        return new Result(true, StatusCode.OK, "分页查询品牌成功！", pageInfo);
    }

    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result search(@RequestBody(required = false) Brand brand, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageInfo pageInfo = brandService.search(brand, pageNum, pageSize);
        return new Result(true, StatusCode.OK, "分页条件查询品牌成功！", pageInfo);
    }


    /**
     * 通过id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "通过id查询品牌成功！", brand);
    }


    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加品牌成功！");
    }


    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable("id") Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "更新品牌成功！");
    }

    /**
     * 通过分类查询品牌列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/category/{categoryId}")
    public Result findBrandListByCategoryId(@PathVariable("categoryId") Integer categoryId) {
        List<Brand> brandList = brandService.findBrandListByCategoryId(categoryId);
        return new Result(true, StatusCode.OK, "查询品牌成功！", brandList);
    }

}
